<?php
require_once("catalog/controller/payment/alipay.php");
require_once("catalog/controller/payment/alipay_function.php");
require_once("catalog/controller/payment/alipay_notify.php");
require_once("catalog/controller/payment/alipay_service.php");
class ControllerAccountRecharge extends Controller {
	private $error = array();
	public function index() {
		if (!$this->customer->isLogged()) {
            $this->redirect($this->url->link('account/login', '', 'SSL'));
        }
		$this->language->load('account/recharge');
		$this->document->setTitle($this->language->get('heading_title'));

        //获得余额
        $this->data['balance']=$this->backend->getbalance($this->customer->getEmail());

        $this->data['text_recharge'] = $this->language->get('text_recharge');
        $this->data['button_continue'] = $this->language->get('button_continue');
        $this->data['button_back'] = $this->language->get('button_back');
        $this->document->addScript('catalog/view/javascript/jquery/tabs.js');

		$this->load->model('account/customer');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

            $price= $this->model_account_customer->getRechargeCodePrice($this->request->post['recharge_code']);

            $this->backend->recharge($this->customer->getEmail(),$this->customer->getPassplain(),$price);

            $this->model_account_customer->UpdateRechargeCodeStatus($this->request->post['recharge_code'],$this->customer->getId());
             $this->session->data['success'] = $this->language->get('text_success');
             $this->data['recharge_success']=true;
        }

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),      	
			'separator' => $this->language->get('text_separator')
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_recharge'),
			'href'      => $this->url->link('account/recharge', '', 'SSL'),      	
			'separator' => $this->language->get('text_separator')
		);

		$this->data['heading_title'] = $this->language->get('heading_title');



        if (isset($this->error['recharge_code'])) {
            $this->data['error_recharge_code'] = $this->error['recharge_code'];
        } else {
            $this->data['error_recharge_code'] = '';
        }



        $this->data['action'] = $this->url->link('account/recharge', '', 'SSL');
        $this->data['online_action'] = $this->url->link('account/recharge/online', '', 'SSL');

        if (isset($this->request->post['recharge_code'])) {
            $this->data['recharge_code'] = $this->request->post['recharge_code'];
        } else {
            $this->data['recharge_code'] = '';
        }



		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/recharge.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/recharge.tpl';
		} else {
			$this->template = 'default/template/account/recharge.tpl';
		}

		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);

		$this->response->setOutput($this->render());	
	}
    public function online(){


        if(!empty($this->request->post['bankcode'])){
            $defaultbank=$this->request->post['bankcode'];
            $paymethod='bankPay';
        }else{
            $defaultbank='';
            $paymethod='';
        }

        $this->load->model('account/recharge');
        $amount=$this->request->post['amount'];
        $seller_email = $this->config->get('alipay_seller_email');
        $security_code = $this->config->get('alipay_security_code');
        $trade_type = $this->config->get('alipay_trade_type');
        $partner = $this->config->get('alipay_partner');
        $currency_code ='CNY';
        $item_name = $this->config->get('config_store');
        $first_name = $this->customer->getFirstName();
        $last_name =  $this->customer->getLastName();
        $currency_value = $this->currency->getValue($currency_code);
        $amount = $amount * $currency_value;

        $amount = number_format($amount,2,'.','');

        $_input_charset = "utf-8";
        $sign_type      = "MD5";
        $transport      = "http";
        $notify_url     = HTTPS_SERVER . 'callback.php';
        $return_url		=HTTPS_SERVER . 'index.php?route=account/transaction/';
        $show_url       = "";

        $recharge_data=array(
            'price'=>$amount,
            'type'=>'alipay',
        );
        $recharge_id=$this->model_account_recharge->addRecharge($recharge_data);
        $order_id=$recharge_id;
        $parameter = array(
            "service"        => $trade_type,
            "partner"        => $partner,
            "return_url"     => $return_url,
            "notify_url"     => $notify_url,
            "_input_charset" => $_input_charset,
            "subject"        => $item_name.' 寄云科技充值' ,
            "body"           => 'Owner ' . $first_name .' '. $last_name,
            "out_trade_no"   => $order_id,
            "price"          => $amount,
            "payment_type"   => "1",
            "quantity"       => "1",
            "logistics_fee"      =>'0.00',
            "defaultbank"	=> $defaultbank,
            "paymethod"	=> $paymethod,
            "logistics_payment"  =>'BUYER_PAY',
            "logistics_type"     =>'EXPRESS',
            "show_url"       => $show_url,
            "seller_email"   => $seller_email
        );







        $alipay = new alipay_service($parameter,$security_code,$sign_type);
        $action=$alipay->build_url();
        $this->id = 'payment';

        header('location:'.$action);
    }
	protected function validate() {
        if (!$this->model_account_customer->getRechargeCodeStatus($this->request->post['recharge_code'])) {
            $this->error['recharge_code'] = "您的充值券错误或者已被使用,如有问题,请联系我们。";
        }
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

}
?>